// ADS1115_WE from Arduino Lib by Wolfgang...
#include "tempUpdate.hpp"
#include "../Global/GlobalDefs.hpp"
//#include "../Global/globalVars.hpp"
//#include "ADS1115_WE/ADS1115_WE.h"
#include <arduino.h>
//#include "Wire.h"
//#include <Wire.h>
// #include "esp_adc_cal.h"

int timeOutOnRead = 0;

float surroundingNTCB;
float surroundingNTCRefR;
float Temp;

bool initADCIfNeeded() {
	return true;
}

float voltMV2tmp(float voltageMV, int channel, bool flagVerbose = false)
{

	// NTC: 100k; divider: 47k(low temp drift)
	const float Rp = 10000.0; // 100K
	const float Rdiv = 10000.0;
	const float T2 = (273.15 + 25.0); // T2
	const float Bx = 3950;          // B
	const float Ka = 273.15;          // K
	const float ov = 3300.0;
	float Rt = (Rdiv * voltageMV) / (ov - voltageMV);
	float firstPart = log(Rt / Rp) / Bx;
	const float secondPart = (1 / T2);
	Temp = 1 / (firstPart + secondPart) - Ka; // ok上面计算原理?NTC原理

	if (ov <= voltageMV || Temp < TEMP_VALID_MIN || Temp > TEMP_VALID_MAX)
	{
#ifdef DEBUG_LOG // no ,continue
		flagVerbose = true;
#endif
		Temp = ERROR_TEMP_CONVERSION_INVALID;
	}
	if (flagVerbose)
	{
		Serial.print("Temp=" + String(Temp));
		Serial.print(", Volt=" + String(voltageMV) + ", Rt=" + String(Rt));
		Serial.println(", firstPart=" + String(firstPart) + ", secondPart=" + String(secondPart));
	}
	return Temp;
}

bool TempReader::init()
{
	pinMode(FAN_TEMP_PIN, INPUT);
	analogSetPinAttenuation(FAN_TEMP_PIN, ADC_11db); //

	pinMode(TANK_TEMP_PIN, INPUT);
	analogSetPinAttenuation(TANK_TEMP_PIN, ADC_11db); //

	return true;
}

float fanVoltBuf = ERROR_TEMP_READ_TIMEOUT;
float TempReader::readFanTmp(bool flagVerbose)
{
	const float updateRatio = 0.3;
	// https://docs.espressif.com/projects/arduino-esp32/en/latest/api/adc.html
	// https://github.com/espressif/arduino-esp32/issues/4941
	// "CircuitPython uses the SDK to set gain to -11dB but I calculated the actual value as -7.5dB based on the ADC being unable to go above 2.6V."
	// float correctionFactorC3 = 1.5;
	// float mv = analogReadMilliVolts(INTERNAL_TEMP_ADC_PIN) * correctionFactorC3;
	float mv = analogReadMilliVolts(FAN_TEMP_PIN);
	//Serial.println("readInternalTmp");
	//Serial.println(mv);
	if (fanVoltBuf == ERROR_TEMP_READ_TIMEOUT)
	{
		fanVoltBuf = mv;
	}
	else
	{
		fanVoltBuf = fanVoltBuf * (1 - updateRatio) + mv * updateRatio;
	}

	float ret = voltMV2tmp(fanVoltBuf, -1, flagVerbose);
	//Serial.println(ret);
	return ret;
}

float tankVoltBuf = ERROR_TEMP_READ_TIMEOUT;
float TempReader::readTankTmp(bool flagVerbose)
{
	const float updateRatio = 0.3;
	// https://docs.espressif.com/projects/arduino-esp32/en/latest/api/adc.html
	// https://github.com/espressif/arduino-esp32/issues/4941
	// "CircuitPython uses the SDK to set gain to -11dB but I calculated the actual value as -7.5dB based on the ADC being unable to go above 2.6V."
	// float correctionFactorC3 = 1.5;
	// float mv = analogReadMilliVolts(INTERNAL_TEMP_ADC_PIN) * correctionFactorC3;
	float mv = analogReadMilliVolts(TANK_TEMP_PIN);
	//Serial.println("readTank");
	//Serial.println(mv);
	if (tankVoltBuf == ERROR_TEMP_READ_TIMEOUT)
	{
		tankVoltBuf = mv;
	}
	else
	{
		tankVoltBuf = tankVoltBuf * (1 - updateRatio) + mv * updateRatio;
	}

	float ret = voltMV2tmp(tankVoltBuf, -1, flagVerbose);
	//Serial.println(ret);
	return ret;
}